﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace C_sports_meet.Common
{
    /// <summary>
    /// ChangePassword.xaml 的交互逻辑
    /// </summary>
    public partial class ChangePassword : Page
    {
        public ChangePassword()
        {
            InitializeComponent();
        }
        private void changePasswordButton_Click(object sender, RoutedEventArgs e)
        {
            string currentPwd = currentPassword.Password;
            string newPwd = newPassword.Password;
            string confirmPwd = confirmPassword.Password;

            // 检查当前密码是否正确
            if (currentPwd != Login.LoggedInPassword)
            {
                resultLabel.Content = "当前密码不正确";
                return;
            }

            if (newPwd == null && currentPwd == null && confirmPwd == null)
            {
                resultLabel.Content = "请把信息填写完整";
                return;
            }

            // 检查新密码是否和确认密码匹配
            if (newPwd != confirmPwd)
            {
                resultLabel.Content = "两次密码不相同";
                return;
            }

            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(@"Server=DESKTOP-EM7AB45;Database=sportmeet;Integrated Security=True"))
                {
                    sqlConnection.Open();

                    string username = Login.LoggedOutUser; // 替换为要更新密码的用户名
                    string rpassword = newPwd; // 新密码

                    string sql = "UPDATE [dbo].[Users] SET PassWord = @Password WHERE UserName = @UserName";

                    using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
                    {
                        sqlCommand.Parameters.AddWithValue("@Password", rpassword);
                        sqlCommand.Parameters.AddWithValue("@UserName", username);
                        int rowsAffected = sqlCommand.ExecuteNonQuery();

                        if (rowsAffected > 0)
                        {
                            resultLabel.Content = "密码更换成功";
                            currentPassword.Password = "";
                            newPassword.Password = "";
                            confirmPassword.Password = "";
                        }
                        else
                        {
                            resultLabel.Content = "密码更换失败";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultLabel.Content = "An error occurred: " + ex.Message;
            }
        }

        private void returnButton_Click(object sender, RoutedEventArgs e)
        {
            Login login = new Login();
            Window.GetWindow(this)?.Close();
            login.Show();
        }
    }
}
